﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.CollectControl.OtherCollectControl.Register
{
    public class PresenterRegisterOtherCollectControl
    {
        IRegisterOtherCollectControl view;
        Model.CollectControl c = new Model.CollectControl();
        Model.OtherCollectControl collectControlToRegister = new Model.OtherCollectControl();
        Model.Kardex k = new Model.Kardex();


        public PresenterRegisterOtherCollectControl(IRegisterOtherCollectControl view)
        {
            this.view = view;
        }

        public Dictionary<string, ICollection<Model.Allotment>> LlenarCbx()
        {
            Dictionary<string, ICollection<Model.Allotment>> manzanos = new Dictionary<string, ICollection<Model.Allotment>>();
            CleanSearch();

            List<Model.Square> squareList = ManagerSquare.GetSquares().ToList();

            squareList = squareList.OrderBy(x => x.SquareId).ToList();

            List<Model.Allotment> activeAllotments = ManagerAllotment.SearchActiveAllotments();

            foreach (Model.Square square in squareList)
            {
                var filteredAllotmentsBySquares = activeAllotments.FindAll(a => a.SquareId.Equals(square.SquareId));
                manzanos.Add(square.SquareId.ToString(), filteredAllotmentsBySquares);
            }
            
            return manzanos;
        }


        public bool RegisterCollect()
        {
            collectControlToRegister.IsPaid = false;
            collectControlToRegister.DateGenerated = DateTime.Now;
            collectControlToRegister.CollectType = 3;              //OtherChargeType->Otros
            collectControlToRegister.UserId = "0";
            collectControlToRegister.KardexId = c.KardexId;
            collectControlToRegister.ControlDetail = view.ControlDetail;
            collectControlToRegister.PaymentAmount = view.PaymentAmount;

            try
            {
                ManagerCollectControl.RegisterOtherCollect(collectControlToRegister);
                return true;
            }
            catch (Exception ex)
            {
                view.NotifyErrorMessage(string.Format("Hubo un error al ingresar los datos a la base de datos.", "Registar Otro Cobro"));
                return false;
            }
        }

        public bool SearchAllotmentInfo()
        {
            bool datosCompletos = true;

            try
            {
                Model.Allotment a = new Model.Allotment();
                CleanSearch();

                try
                {
                    a.AllotmentName = view.Lote;
                    a.SquareId = view.Manzano;

                    a = ManagerKardex.GetAllotmentByAllotmentNameAndSquareId(a);
                    
                    if (k == null)
                        datosCompletos = false;
                    else
                    {
                        c.KardexId = a.KardexId.Value;
                        k.KardexId = c.KardexId;
                        k = ManagerKardex.GetKardexByKardexId(k);
                    }
                }
                catch (Exception ex)
                {
                    datosCompletos = false;
                }

                if (datosCompletos)
                {
                    view.NombreSocio = k.Person.Names + " " + k.Person.LastNames;
                    view.CiSocio = k.Person.PersonId;
                }
                else
                    throw new Exception("No se encontraron resultados de la busqueda...");

                return true;
            }
            catch (Exception ex)
            {
                view.NotifyErrorMessage(ex.Message);
                CleanSearch();
                return false;
            }
        }

        private void CleanSearch()
        {
            view.NombreSocio = "-";
            view.CiSocio = "-";
        }
    }
}
